package com.cgbl.cg.vm.project

import android.app.Application
import android.text.TextUtils
import androidx.databinding.ObservableField
import api.ProjectServiceFactory
import api.UserServiceFactory
import com.robot.base.base.vm.GLBaseViewModel
import com.robot.base.common.api.AbstractViewModelSubscriber
import com.robot.base.configs.AppConstants
import com.robot.base.model.ProjectDetailBean
import com.robot.base.model.UserInfoBean
import com.robot.base.util.arouter.ARouterUtil
import me.goldze.mvvmhabit.binding.command.BindingAction
import me.goldze.mvvmhabit.binding.command.BindingCommand
import me.goldze.mvvmhabit.http.BaseResponse

class MyProjectDetailViewModel(application: Application) : GLBaseViewModel(application) {
    var id = ""

    /**
     * 0审核中，1：待启动，2：已付款，3：开发中，4：已完成，5，已结算，6：审核拒绝
     */
    val type = ObservableField<Int>(0)

    // 支付状态，0:未付款，1：预付款已付，2：全款已付
    val payStatus = ObservableField(0)

    val status = ObservableField<String>()

    val name = ObservableField<String>()
    val budget = ObservableField<String>()
    val time = ObservableField<String>()
    val projectType = ObservableField<String>()
    val phone = ObservableField<String>()
    val describe = ObservableField<String>()

    /**
     * 已关闭原因
     */
    val remake = ObservableField<String>("您的需求不通过已被后台关闭")

    val remakeTime = ObservableField<String>()

    val kfName = ObservableField<String>()

    /**
     * 预付款
     */
    val price = ObservableField<String>()

    //再次发布
    val confirmReleaseCommand = BindingCommand<Any>(BindingAction {
        ARouterUtil.goToActivity(AppConstants.Router.PROJECT.A_RELEASE_PROJECT)
    })

    /**
     * 群聊沟通
     */
    val toChatCommand = BindingCommand<Any>(BindingAction {

    })

    /**
     * 支付尾款
     */
    val toPayCommand = BindingCommand<Any>(BindingAction {

    })

    fun getData() {
        ProjectServiceFactory.getProjectDetail(id)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<ProjectDetailBean>>(this) {
                override fun onNext(t: BaseResponse<ProjectDetailBean>) {
                    initData(t.data)
                }
            })
    }

    fun initData(bean: ProjectDetailBean) {
        name.set(bean.itemName)
        budget.set(bean.itemBudget)
        time.set(bean.itemCycle)
        phone.set(bean.itemPhone)
        describe.set(bean.itemInfo)
        projectType.set(bean.labelName)
        type.set(bean.status)
        if (bean.status == 6) {
            type.set(-1)
        }
        status.set(
            when (bean.status) {
                //0审核中，1：待启动，2：已付款，3：开发中，4：已完成，5，已结算，6：审核拒绝
                0 -> {
                    "审核中"
                }
                1 -> {
                    "待启动"
                }
                2 -> {
                    "已付款"
                }
                3 -> {
                    "开发中"
                }
                4 -> {
                    "已完成"
                }
                5 -> {
                    "已结算"
                }
                6 -> {
                    "审核拒绝"
                }
                else -> {
                    ""
                }
            }
        )
        if (!TextUtils.isEmpty(bean.customerAccountId)) {
            queryOtherUserInfo(bean.customerAccountId ?: "")
        }
    }

    fun queryOtherUserInfo(userId: String) {
        UserServiceFactory.otherUserInfo(userId)
            .subscribe(object : AbstractViewModelSubscriber<BaseResponse<UserInfoBean>>(this) {
                override fun onNext(t: BaseResponse<UserInfoBean>) {
                    kfName.set(t.data.nickName)
                }
            })
    }
}